import { setGlobals } from '@swda/utils';
//==============================================================================================================
//-----------------------------------------------------------------------------
// Window_VBOptions

function Window_VBOptions() {
  this.initialize.apply(this, arguments);
}

Window_VBOptions.prototype = Object.create(Window_Command.prototype);
Window_VBOptions.prototype.constructor = Window_VBOptions;

Window_VBOptions.prototype.initialize = function () {
  Window_Command.prototype.initialize.call(this, 0, 0);
  this.updatePlacement();
  this._backgroundSprite = new Sprite();
  this._backgroundSprite.bitmap = ImageManager.loadBitmap(
    "img/system/UI/",
    "MenuBg_OptionsList"
  );
  this.addChildAt(this._backgroundSprite, 0);
  this._backgroundSprite.x = -17;
  this._backgroundSprite.y = -1;
  this.opacity = 0;
};

Window_VBOptions.prototype.windowWidth = function () {
  return 384;
};

Window_VBOptions.prototype.statusWidth = function () {
  return 120;
};

Window_VBOptions.prototype.windowHeight = function () {
  return this.fittingHeight(Math.min(this.numVisibleRows(), 12));
};

Window_VBOptions.prototype.numVisibleRows = function () {
  return 8;
};

Window_VBOptions.prototype.updatePlacement = function () {
  this.x = (Graphics.boxWidth - this.width) / 2;
  this.y = (Graphics.boxHeight - this.height) / 2;
};

Window_VBOptions.prototype.makeCommandList = function () {
  this.addGeneralOptions();
};


// 固定方向摇杆/全屏幕触摸/布局设定
Window_VBOptions.prototype.addGeneralOptions = function () {
  this.addCommand("使用虚拟按键", "vbOn");
  this.addCommand("固定方向摇杆", "vbRockerFix", this.vbEnabled());
  this.addCommand("全屏触摸", "vbCoexistTouch", this.changeCoexistTouchEnabled());
  this.addCommand("按键布局", "vbKeyPos", this.vbEnabled());
};

Window_VBOptions.prototype.vbEnabled = function () {
  return this.getConfigValue("vbOn");
};


Window_VBOptions.prototype.changeCoexistTouchEnabled = function () {
  return this.getConfigValue("vbRockerFix") && this.vbEnabled();
};

// BGM和ME合并；BGS和SE合并
Window_VBOptions.prototype.drawItem = function (index) {
  var rect = this.itemRectForText(index);
  var statusWidth = this.statusWidth();
  var titleWidth = rect.width - statusWidth;
  this.resetTextColor();
  this.changePaintOpacity(this.isCommandEnabled(index));
  this.drawText(this.commandName(index), rect.x, rect.y, titleWidth, "left");
  this.setSmallerFont();
  this.contents.fontSize = 20;
  this.changeTextColor(this.textColor(23));
  this.drawText(
    this.statusText(index),
    titleWidth,
    rect.y,
    statusWidth,
    "right"
  );
  this.resetFontSettings();
};

Window_VBOptions.prototype.statusText = function (index) {
  var symbol = this.commandSymbol(index);
  var value = this.getConfigValue(symbol);
  if (
    symbol == "vbOn" ||
    symbol == "vbCoexistTouch" ||
    symbol == "vbRockerFix"
  ) {
    return this.booleanStatusText(value);
  }
  return ""
};

//更改文字
Window_VBOptions.prototype.booleanStatusText = function (value) {
      return value ? "开" : "关";
};

Window_VBOptions.prototype.processOk = function () {
  var index = this.index();
  var symbol = this.commandSymbol(index);
  var value = this.getConfigValue(symbol);
  if (this.isCurrentItemEnabled()) {
    if( symbol == "vbOn"){
      this.changeValue(symbol, !value);
      // 若关闭虚拟摇杆，则强制开启全屏触摸
      if(value)  this.changeValue("vbCoexistTouch", true);
      this.refresh();
    } else if ( symbol == "vbCoexistTouch") {
      this.changeValue(symbol, !value);
    } else if ( symbol == "vbRockerFix") {
      this.changeValue(symbol, !value);
      // 若不固定摇杆，则强制关闭全屏触摸
      if(value)  this.changeValue("vbCoexistTouch", false);
      this.refresh();
    }else{
      Window_Command.prototype.processOk.call(this);
    }
  }else{
    this.playBuzzerSound();
  }
};

Window_VBOptions.prototype.statusRect = function (index) {
  var rect = this.itemRectForText(index);
  var statusWidth = this.statusWidth();
  rect.x = rect.width - statusWidth - 56;
  rect.width = statusWidth;
  return rect;
};


Window_VBOptions.prototype.cursorRight = function (wrap) {
  var index = this.index();
  var symbol = this.commandSymbol(index);
  if (this.isCurrentItemEnabled()) {
    if( symbol == "vbOn"){
      this.changeValue(symbol, true);
      // 若开启虚拟摇杆，不用变更全屏触摸
      this.refresh();
    } else if ( symbol == "vbCoexistTouch") {
      this.changeValue(symbol, true);
    } else if ( symbol == "vbRockerFix") {
      this.changeValue(symbol, true);
      // 不固定摇杆，不用变更全屏触摸
      this.refresh();
    }
  }
};

Window_VBOptions.prototype.cursorLeft = function (wrap) {
  var index = this.index();
  var symbol = this.commandSymbol(index);
  if (this.isCurrentItemEnabled()) {
    if( symbol == "vbOn"){
      this.changeValue(symbol, false);
      // 若关闭虚拟摇杆，则强制开启全屏触摸
      this.changeValue("vbCoexistTouch", true);
      this.refresh();
    } else if ( symbol == "vbCoexistTouch") {
      this.changeValue(symbol, false);
    } else if ( symbol == "vbRockerFix") {
      this.changeValue(symbol, false);
      // 不固定摇杆，强制关闭全屏触摸
      this.changeValue("vbCoexistTouch", false);
      this.refresh();
    }
  }
};

Window_VBOptions.prototype.changeValue = function (symbol, value) {
  const lastValue = this.getConfigValue(symbol);
  if (lastValue !== value) {
    this.setConfigValue(symbol, value);
    this.redrawItem(this.findSymbol(symbol));
    SoundManager.playCursor();
  }
};

Window_VBOptions.prototype.getConfigValue = function (symbol) {
  return ConfigManager[symbol];
};

Window_VBOptions.prototype.setConfigValue = function (symbol, value) {
  ConfigManager[symbol] = value;
};

// ===========================================================================================================
// Scene_VBOptions
function Scene_VBOptions() {
  this.initialize.apply(this, arguments);
}

Scene_VBOptions.prototype = Object.create(Scene_Options.prototype);
Scene_VBOptions.prototype.constructor = Scene_VBOptions;

Scene_VBOptions.prototype.initialize = function() {
  Scene_Options.prototype.initialize.call(this);
};

Scene_VBOptions.prototype.create = function () {
  Scene_MenuBase.prototype.create.call(this);//这里必须是Scene_MenuBase
  this.createOptionsWindow();
  this.createInstructionWindow();
};
Scene_VBOptions.prototype.terminate = function() {
  Scene_Options.prototype.terminate.call(this);
  ConfigManager.save();        //保存设定
  VB_Manager.changeVisible(ConfigManager['vbOn']);
  VB_Manager.refreshConfig();  //更新虚拟摇杆设置
};

Scene_VBOptions.prototype.createOptionsWindow = function () {
  this._optionsWindow = new Window_VBOptions();
  this._optionsWindow.setHandler("cancel", this.popScene.bind(this));
  this._optionsWindow.setHandler("vbKeyPos", this.commandVbKeyPos.bind(this));
  this.addWindow(this._optionsWindow);
};

Scene_VBOptions.prototype.createInstructionWindow = function() {
  const text = "虚拟摇杆设定将在退出该菜单时生效。";
  const tw = 17;
  const width = tw * Window_Base.prototype.standardFontSize() + Window_Base.prototype.textPadding() * 2 + Window_Base.prototype.standardPadding() * 2;
  const height = Window_Base.prototype.standardFontSize()  + Window_Base.prototype.textPadding() * 2 + Window_Base.prototype.standardPadding() * 2;
  const x = (Graphics.boxWidth - this._optionsWindow.width) / 2 - 40;
  const y = (Graphics.boxHeight - this._optionsWindow.height) / 2 - height;
  //const [x, y] = [Graphics.boxWidth - width - 16, 16];
  this._instructionWindow = new Window_Instruction(x, y, width, height);
  this._instructionWindow.setText(text);
  this.addWindow(this._instructionWindow);
};

Scene_VBOptions.prototype.commandVbKeyPos = function () {
  SceneManager.push(Scene_DebugVB);
};

setGlobals([
    ['Window_VBOptions',Window_VBOptions],
    ['Scene_VBOptions',Scene_VBOptions],
])
